package com.sdx.console.base.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.util.DateUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.AuthorizedBaesController;
import com.ibeetl.admin.core.web.JsonResult;
import com.sdx.common.utils.JacksonUtils;
import com.sdx.common.utils.Utils;
import com.sdx.console.base.entity.BaseSuggest;
import com.sdx.console.base.service.BaseSuggestService;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 意见反馈 接口
 */
@RequestMapping("/base/baseSuggest")
@Controller
public class BaseSuggestController extends AuthorizedBaesController{
	private static final Logger log = LoggerFactory.getLogger(BaseSuggestController.class);
    private static final String MODEL = "/base/baseSuggest";


    @Resource
	private BaseSuggestService baseSuggestService;

	/**
	 * 初始化页面需要的列表数据，
	 *
	 */
	private void setPageParam(ModelAndView view,HttpServletRequest request) {

	}

    /* 页面 */

    @GetMapping("/index.do")
    @Function("base.baseSuggest.query")
    @ResponseBody
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/index.html") ;
		setPageParam(view,request);
        return view;
    }

    @GetMapping("/edit.do")
    @Function("base.baseSuggest.edit")
    @ResponseBody
    public ModelAndView edit(HttpServletRequest request,Integer suggestId) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
        BaseSuggest baseSuggest = baseSuggestService.queryById(suggestId);
        view.addObject("baseSuggest", baseSuggest);
		setPageParam(view,request);
		try {
			view.addObject("baseSuggestJson", JacksonUtils.toJsonString(baseSuggest,JacksonUtils.DF1));
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
        return view;
    }

    @GetMapping("/add.do")
    @Function("base.baseSuggest.add")
    @ResponseBody
    public ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(MODEL+"/add.html");
		setPageParam(view,request);
        return view;
    }

    /* ajax json */

    @PostMapping("/list.json")
    @Function("base.baseSuggest.query")
    @ResponseBody
    public JsonResult<PageQuery> list(BaseSuggest condtion)
    {
        PageQuery page = condtion.getPageQuery();
        baseSuggestService.queryByCondition(page);
        return JsonResult.success(page);
    }

    private JsonResult save(boolean isNew,BaseSuggest baseSuggest,HttpServletRequest request
	) {
    	JsonResult result = JsonResult.success();
		String dir = DateUtil.now("yyyyMMdd");
    	String subPath = "/images/cover/"+dir+"/";
		String bucketName = "sdxcomm";
		CoreUser user = getLoginUser(request);

    	if (isNew) {
			baseSuggest.setCreatedTime(new Date());
			baseSuggestService.save(baseSuggest);
		} else {
			baseSuggestService.updateTemplate(baseSuggest);
		}
		return result;
	}
    @PostMapping("/add.json")
    @Function("base.baseSuggest.add")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)BaseSuggest baseSuggest,HttpServletRequest request
		)
    {

        return save(true,baseSuggest,request
		);
    }

    @PostMapping("/update.json")
    @Function("base.baseSuggest.edit")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  BaseSuggest baseSuggest,HttpServletRequest request
		) {
		return save(false,baseSuggest,request
		);
    }



    @GetMapping("/view.json")
    @Function("base.baseSuggest.query")
    @ResponseBody
    public JsonResult<BaseSuggest>queryInfo(Integer suggestId) {
        BaseSuggest baseSuggest = baseSuggestService.queryById( suggestId);
        return  JsonResult.success(baseSuggest);
    }

    @PostMapping("/delete.json")
    @Function("base.baseSuggest.del")
    @ResponseBody
    public JsonResult delete(String ids) {
		List<String> idList = Utils.strToList(ids,",");
        baseSuggestService.batchDelBaseSuggest(idList);
        return JsonResult.success();
    }

    @PostMapping("/check.json")
    @ResponseBody
    public JsonResult check( BaseSuggest baseSuggest,HttpServletRequest request) {
        CoreUser user = getLoginUser(request);
        baseSuggest.setStatus(1);
        baseSuggest.setUpdatedBy(user.getId().intValue());
        baseSuggest.setUpdatedName(user.getName());
        baseSuggest.setUpdatedTime(new Date());
        baseSuggestService.updateTemplate(baseSuggest);
        return JsonResult.success();
    }

    @GetMapping("/showImg.do")

    @ResponseBody
    public ModelAndView showImg(HttpServletRequest request,Integer suggestId) {
        ModelAndView view = new ModelAndView(MODEL+"/showImg.html");
        setPageParam(view,request);
//        suggestId
        BaseSuggest baseSuggest = baseSuggestService.queryById(suggestId);
        String file = baseSuggest.getFile();
        if(StringUtils.isNotEmpty(file)){
            List<String> list = new ArrayList<>();
            String[] split = file.split("@");
            for (String s : split) {
                list.add(s);
            }
            view.addObject("imgs",list);
        }
        return view;
    }


}
